package net.xju3_4.dao;

import net.xju3_4.common.utils.JdbcUtilsV1;
import net.xju3_4.common.utils.PageInfo;
import net.xju3_4.entity.MerchantApplicationEntity;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;


import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MerchantApplicationDao {
    private final QueryRunner queryRunner = new QueryRunner();

    // 插入商家申请
    public int insert(MerchantApplicationEntity application) throws SQLException {
        Connection conn= JdbcUtilsV1.getConnection();
        String sql = "INSERT INTO merchant_application " +
                "(username, password, merchant_name, contact_person, contact_phone, " +
                "address, business_license, logo_url, description, status) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        Object[] params = {
                application.getUsername(),
                application.getPassword(),
                application.getMerchantName(),
                application.getContactPerson(),
                application.getContactPhone(),
                application.getAddress(),
                application.getBusinessLicense(),
                application.getLogoUrl(),
                application.getDescription(),
                application.getStatus()
        };

        int count=JdbcUtilsV1.executeUpdate(conn, sql, params);
        return count;
    }

    // 根据ID查询申请
    public MerchantApplicationEntity getById(Integer id) throws SQLException {
        Connection conn= JdbcUtilsV1.getConnection();
        String sql = "SELECT * FROM merchant_application WHERE id = ?";
        MerchantApplicationEntity applications =JdbcUtilsV1.executeQueryOne(conn,MerchantApplicationEntity.class,sql, id);
        return applications;
    }


    public PageInfo<MerchantApplicationEntity> pagelist(Integer pageNo, Integer pageSize, MerchantApplicationEntity application) {
       String sql="select " + "id, " + "username, " + "password, " +
               "merchant_name as merchantName, " +  // 映射到实体类 merchantName
               "contact_person as contactPerson, " +  // 映射到实体类 contactPerson
               "contact_phone as contactPhone, " +  // 映射到实体类 contactPhone
               "address, " +
               "business_license as businessLicense, " +  // 映射到实体类 businessLicense
               "logo_url as logoUrl, " +  // 映射到实体类 logoUrl
               "description, " +
               "status " +
               "from merchant_application where ";
        List <Object> params=new ArrayList<>();
        if(application.getUsername()!=null)
        {
            sql=sql+"username like ? and ";
            params.add("%"+application.getUsername()+"%");
        }
        if(application.getMerchantName()!=null)
        {
            sql=sql+"merchant_name like ? and ";
            params.add("%"+application.getMerchantName()+"%");
        }
        if (application.getAddress()!=null){
            sql=sql+"address like ? ";
            params.add("%"+application.getAddress()+"%");
        }
        sql += " limit ?,?";
        params.add((pageNo-1)*pageSize);
        params.add(pageSize);
        List<MerchantApplicationEntity> applications = JdbcUtilsV1.executeQuery(MerchantApplicationEntity.class,sql,params.toArray());


       String  sqlcount="select count(*) from merchant_application  ";
        Long count=JdbcUtilsV1.executeQueryOneValue(JdbcUtilsV1.getConnection(),sqlcount);
        PageInfo<MerchantApplicationEntity> pageInfo = new PageInfo<>(pageNo,pageSize, Math.toIntExact(count),applications);

         return pageInfo;

    }

    public Integer accept(Integer id) {
        String sql="update merchant_application set status=1 where id=?";
        int count=JdbcUtilsV1.executeUpdate(sql,id);
        if(count==0) return 0;
        sql="select " + "id, " + "username, " + "password, " +
                "merchant_name as merchantName, " +  // 映射到实体类 merchantName
                "contact_person as contactPerson, " +  // 映射到实体类 contactPerson
                "contact_phone as contactPhone, " +  // 映射到实体类 contactPhone
                "address, " +
                "business_license as businessLicense, " +  // 映射到实体类 businessLicense
                "logo_url as logoUrl, " +  // 映射到实体类 logoUrl
                "description, " +
                "status " +
                "from merchant_application where id=?";
        MerchantApplicationEntity application=JdbcUtilsV1.executeQueryOne(JdbcUtilsV1.getConnection(),MerchantApplicationEntity.class,sql,id);
        String insertSql = "INSERT INTO merchant (" +
                "username, password, merchant_name, contact_person, contact_phone, " +
                "address, business_license, description, logo_url, status" +
                ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        // 3. 准备参数（注意：不要嵌套列表）
        Object[] params = new Object[] {
                application.getUsername(),
                application.getPassword(),
                application.getMerchantName(),
                application.getContactPerson(),
                application.getContactPhone(),
                application.getAddress(),
                application.getBusinessLicense(),
                application.getDescription(),
                application.getLogoUrl(),
                1 // 状态为已通过
        };

        // 4. 执行插入（关键：直接传递 params 数组）
         count = JdbcUtilsV1.executeUpdate(insertSql, params);

        return count;
    }

    public int reject(Integer id) {
        String sql="update merchant_application set status=2 where id=?";
        Integer count=JdbcUtilsV1.executeUpdate(sql,id);
        return count;
    }

    public int deleted(Integer id) {
        String sql="delete from merchant_application where id=?";
        Integer count=JdbcUtilsV1.executeUpdate(sql,id);
        return count;
    }
}
